import {BaseEle} from "./core.js";

class CMessageCom extends BaseEle {
    constructor() {
        super();
        let styles = this.getAttribute("styles");

        let links = []
        styles.split(";").forEach(style => {
            links.push(`<link rel="stylesheet" href="${style}" />`);
        })

        this.innerHTML = `
                           <z-dialog
                        id="dialogprompt"
                        dialog-title=""
                        confirm-text="确认"
                        style="--z-dialog-width: 300px"
                >
                    <template name="prerender">
                              ${links.join("\n")}          
                    </template>
                    <template name="content">
                        <z-fcinput
                                bind:change="handlePriceChange"
                                :value="textData" ></z-fcinput>
                    </template>
                </z-dialog>
                            <z-dialog
                        id="dialogconfirm"
                        dialog-title=""
                        confirm-text="确认"
                        style="--z-dialog-width: 300px"
                >
                    <template name="prerender">
                              ${links.join("\n")}          
                    </template>
                    <template name="content">
                    </template>
                </z-dialog>
            `;

        let self = this;
        window.showprompt = function (title) {
            return new Promise((resolve, reject) => {
                let element = self.querySelector(':scope #dialogprompt');
                element.setAttribute('dialog-title', title);
                let app = element.showModalApp({
                    data() {
                        return {
                            textData: ''
                        }
                    },
                    methods: {
                        handlePriceChange(e) {
                            let val = e.target.value;
                            console.log('m-echarts', val)
                            app.setData('textData', val);
                        },
                    }
                });
                function handleConfirm(e) {
                    resolve(app.getData('textData'));
                    element.removeEventListener('confirm', handleConfirm);
                }
                element.addEventListener('confirm', handleConfirm)
            })
        }
        window.showconfirm = function (title) {
            return new Promise((resolve, reject) => {
                let element = self.querySelector(':scope #dialogconfirm');
                element.setAttribute('dialog-title', title);
                let res = false
                let app = element.showModalApp({
                    data() {
                        return {
                        }
                    },
                    methods: {
                    }
                });
                function handleConfirm(e) {
                    res = true;
                }
                element.addEventListener('confirm', handleConfirm)
                function handleComp(e) {
                    resolve(res);
                    element.removeEventListener('hide', handleComp);
                    element.removeEventListener('confirm', handleConfirm);
                }
                element.addEventListener('hide', handleComp)

            })
        }
    }

}

CMessageCom.defsel('z-message-com', {
    attrs: {}
})